<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/2/15
 * Time: 15:17
 * title: 官方赠送记录
 */

namespace app\admin\controller;
use app\admin\model\User;
use app\admin\model\GivePresen as GivePresenModel;
class GivePresen extends Index
{

    public function index()
    {
        return view();
    }

    public function indexData()
    {
        $givePresenModel = new GivePresenModel();
        $data = $givePresenModel->getDataList();
        return json($data);
    }
    public function givePresenGiftName()
    {
        $givePresenModel = new GivePresenModel();
        $data = $givePresenModel->giftName();
        return json($data);
    }

    public function givePresenAdd()
    {

        $givePresenModel = new GivePresenModel();
        $res = $givePresenModel->givePresenAdd();
        return $res;


    }

    /**
     * 动作---提交审批
     * 官方赠送
     * @return array
     */
    public function largess()
    {
        $arr = [26,68,56];
        if( !in_array(session('admin_id'),$arr)){
            return ['code'=>-100,'msg'=>'您没有权限'];
        }
        $givePresenModel = new GivePresenModel();
        $res = $givePresenModel->largess();
        return $res;

    }

    /**
     * 审批
     * @return mixed
     */
    public function givePresenApprove()
    {
        $givePresenModel = new GivePresenModel();
        $res = $givePresenModel->approve();
        return $res;
    }

    /**
     * 审批view
     * @return \think\response\Json
     */
    public function approveData()
    {
        $givePresenModel = new GivePresenModel();
        $data = $givePresenModel->getApproveList();
        return json($data);
    }

    /**
     * 动作---审核
     * 审核成功/失败
     */
    public function approveCheck()
    {
        $arr = [23,24,38];
        if( !in_array(session('admin_id'),$arr)){
            return ['code'=>-100,'msg'=>'您没有权限'];
        }
        $familyModel = new GivePresenModel();
        $res = $familyModel->approveCheck();
        if($res) return ['code'=>$res,'msg'=>'审批成功'];
        else return ['code'=>-100,'msg'=>'审批失败'];
    }

    /**
     * 审批后台充值
     * @return \think\response\View
     */
    public function approveView()//GivePresenApproveView
    {
        $id = (int)input('id');
        $givePresenModel = new GivePresenModel();
        $ct = $givePresenModel->where('id','=',$id)->where('status',0)->value('create_time');
        $data = $givePresenModel->where('create_time','eq',$ct)->where('status',0)->select();

        $mydata=[];
        $nickname = User::where('id','=',$data[0]['user_id'])->value('nickname');
        $ID = User::where('id','=',$data[0]['user_id'])->value('id');
        $U_ID = User::where('id','=',$data[0]['user_id'])->value('u_id');
        if($data->count() === 1){
            $mydata['user_id'] = $data[0]['user_id'];
            $mydata['ID'] = $ID;
            $mydata['nickname'] = $nickname;
            $mydata['gold'] = $data[0]['prize_type'] === 1 ? $data[0]['prize_number'] : 0 ;
            $mydata['diamond'] = $data[0]['prize_type'] === 2 ? $data[0]['prize_number'] : 0 ;
            $mydata['reason'] = explode('%',$data[0]['remark'])[1];
            $mydata['operator'] = explode(':',$data[0]['remark'])[1];
            $mydata['ct'] = date('Y-m-d H:i:s',$ct);
            $mydata['ctime'] = $ct;
            $mydata['u_id'] = $U_ID;
        }else{
            $mydata['user_id'] = $data[0]['user_id'];
            $mydata['nickname'] = $nickname;
            $mydata['gold'] = $data[0]['prize_type'] === 1 ? $data[0]['prize_number'] : ($data[1]['prize_type'] === 1 ? $data[1]['prize_number'] : 0 );
            $mydata['diamond'] = $data[1]['prize_type'] === 2 ? $data[1]['prize_number'] : ($data[0]['prize_type'] === 2 ? $data[0]['prize_number'] : 0 );
            $mydata['ID'] = $ID;
            $mydata['reason'] = explode('%',$data[0]['remark'])[1];
            $mydata['operator'] = explode(':',$data[0]['remark'])[1];
            $mydata['ct'] = date('Y-m-d H:i:s',$ct);
            $mydata['ctime'] = $ct;
            $mydata['u_id'] = $U_ID;
        }

        return view()->assign('data',$mydata);
    }



}